<!DOCTYPE html>
<html>
<head>
    <title>找攻略</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategy.css"/>
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <script src="/js/plugins/jrender/jrender.js"></script>
	<script>

        var tabs = [] ;//记录当前标签页的信息
        var currentTab ;//当前查看的标签页信息

        //创建一个选项卡的方法
        function createTab(index, obj) {
            /*<li class="nav-item" data-index="xx">
            <a data-tid="xx" class="nav-link" data-toggle="pill" href="#pills-xx">oo</a>
                    </li>*/
            var a = $("<a class='nav-link' data-toggle='pill'></a>").attr("data-tid", obj.id)
                .attr("href", "#pills-"+obj.id).html(obj.name);
            return $("<li class='nav-item'></li>").attr("data-index", index).append(a);
        }

        //定义一个函数，判断当前溢出高度加上当前的窗口高度是否大于整个页面的高度
        function isEnd() {
            //返回true或者false
            return $(window).height() + $(window).scrollTop() >= $(document).height();
        }

        $(function () {
            //页面加载完成之后，发送get请求，获取指定的唯一一篇的攻略推荐
            $.get("/travels/commends?type="+3,function (data) {
                $(".commend").renderValues(data[0],{
                    handler:function (ele,value) {
                        ele.href = "/travelContent.html?id="+value ;
                    }
                });
            })
            //页面加载完之后获取所有被推荐的大攻略
            $.get("/strategys",{state:2,pageSize:0},function (data) {
                //渲染数据
                $(".strategyCommend").renderValues(data,{
                    handle:function (ele, value) {
                        ele.href = "/strategyCatalogs.html?strategyId="+value ;
                    }
                });
            })

            //渲染选项卡
            $.get("/regions",{state:1},function (data) {
                $.each(data,function (index, ele) {
                    var tab = createTab(index,ele);
                    tab.appendTo("#pills-tab");

                    //同步查询出对应标签页的内容
                    $.get("/regions/"+ele.id+"/strategys",function (data) {
                        $(data).appendTo("#pills-tabContent");

                        tabs[index] = {id:ele.id,currentPage:1,pages:$(data).data("pages")};
                    },"html")
                })

            })
            //动态绑定事件
            $("#pills-tab").on("click","a[data-tid]",function () {
                //拿到当前标签页信息，从a标签的父元素li中取出自定义Index的属性值
                var index = $(this).closest("li").data("index");
                currentTab = tabs[index];
            });
            //设置窗口的滚动事件，用于翻页
            $(window).scroll(function () {
                if(isEnd()){
                    if(currentTab.currentPage < currentTab.pages){
                        //如果没有到达底部的话，就当前页自增之后，发送get请求，获取第二页的数据
                        currentTab.currentPage ++ ;
                        $.get("/regions/"+currentTab.id+"/strategys",{currentPage:currentTab.currentPage},function (data) {
                            //把回显的内容渲染到对应的标签页中
                            $(data).appendTo("#pills-"+currentTab.id+" .classify");
                        },"html")
                    }else{
                        //如果滚动到了底部就给个友好的提示，使用dialog这个插件进行有好的提示
                        $(document).dialog({
                            type : 'notice',
                            infoText: "已经到底了，兄地,再滚就穿了",
                            autoClose: 1500,
                            position: 'bottom'  // center: 居中; bottom: 底部
                        });
                    }
                }
            })

            //搜索实现
            $("#searchBtn").click(function () {
                var keyword = $(".search input").val();
                //带keyword参数到搜索结果页面进行搜索
                //对古按键子编码
                keyword = escape(keyword);
                location.href="/searchResult.html?type=strategy&keyword="+keyword;

            })
        });
    </script>
</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="index.html">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="col-8" placeholder="搜索攻略">
                <a id="searchBtn" style="color: #ffffff" class="col-2">搜索</a>
            </div>
        </div>
    </div>
</div>

<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab" role="tablist">
    <!--选项卡？？？？？？？？？？？？？？？？？？？？？-->
    <li class="nav-item">
        <a class="nav-link active show" id="pills-top-tab" data-toggle="pill" href="#pills-top">
            <span style="font-weight: bold;">推荐</span>
        </a>
    </li>

</ul>

<div class="tab-content" id="pills-tabContent">
    <div class="tab-pane fade active show" id="pills-top">
        <div class="container commend">
            <a render-key="travelId" render-fun="handler">
                <img render-src="coverUrl">
                <p render-html="title"></p>
            </a>
        </div>
        <hr>
        <div class="container strategyCommend">
            <h6>推荐</h6>
            <div class="row hot" render-loop="list">
                <div class="col-4">
                    <a render-key="list.id" render-fun="handle">
                        <img render-src="list.coverUrl">
                        <p render-html="list.title"></p>
                    </a>
                </div>
            </div>
        </div>
    </div>

</div>
</body>
</html>